package object2D;

import context.*;

import model2D.Model2D;
import model2D.ObjectTableModel;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import util.PixelPoint;
import util.Util2;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.util.Vector;
import java.util.Enumeration;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;

// ********************************************************************
// *** CLASS: GroupObject
// ********************************************************************
public class GroupObject extends Object2D implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="object2D.GroupObject";
  static final String TYPE="Group";

  private Vector<Object2D> children=new Vector<Object2D>();

  public GroupObject(Model2D objectModel) throws JSONException
  { super(objectModel,null);
    objectModel.setCurrentGroup(this);
  }
  
  public GroupObject(Model2D objectModel,JSONObject jsonObject) throws JSONException
  { super(objectModel,jsonObject.getString("id"));
	if(DEBUG) Log.d(TAG,"<init>: jsonObject="+jsonObject);
    objectModel.setCurrentGroup(this);
	JSONArray jsonChildren=jsonObject.getJSONArray("children");
    if(DEBUG) Log.d(TAG,"<init>: children="+jsonChildren.toString(4));
    for(int i=0;i<jsonChildren.length();i++)
    { JSONObject jsonChild=jsonChildren.getJSONObject(i);
      if(DEBUG) Log.d(TAG,"<init>: child="+jsonChild.toString(4));
      String type=jsonChild.getString("type");
      //if(DEBUG) Log.d(TAG,"buildObjectModelFromJsonTree: type="+type);
      try
      { if(type.equals(LineObject.TYPE)) addChild(new LineObject(objectModel,jsonChild));
        else if(type.equals(PathObject.TYPE)) addChild(new PathObject(objectModel,jsonChild));
        else if(type.equals(EllipseObject.TYPE)) addChild(new EllipseObject(objectModel,jsonChild));
        else if(type.equals(TextObject.TYPE)) addChild(new TextObject(objectModel,jsonChild));
        else if(type.equals(ImageObject.TYPE)) addChild(new ImageObject(objectModel,jsonChild)); 
        else if(type.equals(GroupObject.TYPE)) addChild(new GroupObject(objectModel,jsonChild));
        else Log.NOT_IMPLEMENTED(TAG,"<init>: UNKNOWN TYPE="+type+", Source="+jsonChild);
      } catch(Exception e) { Log.e(TAG,"<init>: FAILED: Source="+jsonChild,e); }
    }
  }
  
  public void addChild(Object2D child)
  {	if(DEBUG) Log.d(TAG,"addChild: child="+child);
	children.add(child);
  }
  
  public void removeChild(Object2D child)
  {	if(DEBUG) Log.d(TAG,"removeChild: child="+child);
	children.remove(child);
  }
  
  public Object2D getObject(PixelPoint p)
  { for(Enumeration<Object2D> e=children.elements();e.hasMoreElements();)
	{ Object2D obj=e.nextElement();
	  if(obj.contains(p)) return(obj);
	}
	return(null);
  }

  public JSONObject toJSONObject() throws JSONException
  { JSONObject jsonObject=new JSONObject();
    jsonObject.put("type",TYPE);
    jsonObject.put("id",getIdent());
    JSONArray arr=new JSONArray();
    for(Enumeration<Object2D> e=children.elements();e.hasMoreElements();)
    { Object2D member=e.nextElement();
      JSONObject child=member.toJSONObject();
      arr.put(child);
    }
    jsonObject.put("children",arr);
	return(jsonObject);
  }

  public ObjectTableModel toObjectTableModel()
  { ObjectTableModel objectTableModel=new ObjectTableModel(children);
	return(objectTableModel);
  }
  

  // *************************************************************************
  // *** METHOD: paint
  // *************************************************************************
  public void paint(Graphics2D g,ImageObserver imageObserver)
  { for(Enumeration e=children.elements(); e.hasMoreElements();)
    { Object2D obj=(Object2D)(e.nextElement());
      obj.paint(g,imageObserver);
    }
  }

  // ****************************************************************
  // *** INNER CLASS: EditGeneralPanel
  // ****************************************************************
  public JPanel getEditGeneralPanel(String title)
  { return(new EditGeneralPanel(title)); }
  private class EditGeneralPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JTextField identField;
    private JButton strokeButton;
    
    public void paint(Graphics g)
    { if(DEBUG) Log.d(TAG,"EditGeneralPanel.paint: ");
      identField.setText(getIdent());
      strokeButton.setBackground(stroke.getColor());
  	  super.paint(g);  
    }
  	
    public EditGeneralPanel(String title)
    { setBorder(Util2.getTitleBorder(title));
      setPreferredSize(new Dimension(350,50+20));
      setLayout(new GridLayout(2,2));
      add(new JLabel("Ident:"));
      identField=new JTextField(getIdent());
      identField.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setIdent(identField.getText()); }
      });
      add(identField);
      strokeButton=addEditStrokeButton(this,true);
    }
  }    

}
